package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.Response;
import com.fqgj.exception.common.ApiErrorException;
import com.fqgj.xjd.user.client.UserAuthorizedCollectService;
import com.fqgj.xjd.user.client.enums.AuthCollectStatusEnum;
import com.fqgj.xjd.user.client.enums.AuthCollectTypeEnum;
import com.fqgj.xjd.user.client.response.UserAuthorizedCollect;
import com.fqgj.xjd.user.dao.UserAuthorizedCollectDao;
import com.fqgj.xjd.user.entity.UserAuthorizedCollectEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户授权采集
 *
 * @author liuming
 * Created on 下午3:56 2019/6/27.
 */
@Service("userAuthorizedCollectService")
public class UserAuthorizedCollectServiceImpl implements UserAuthorizedCollectService {

    @Autowired
    UserAuthorizedCollectDao userAuthorizedCollectDao;

    @Override
    public Response<List<UserAuthorizedCollect>> getByUserCode(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            throw new ApiErrorException("User code is empty");
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("userCode", userCode);
        List<UserAuthorizedCollectEntity> list = userAuthorizedCollectDao.selectByParams(map);
        if (list == null || list.isEmpty()) {
            return Response.ok().putData(null);
        }
        return Response.ok().putData(convertEntityToVO(list));
    }

    private List<UserAuthorizedCollect> convertEntityToVO(List<UserAuthorizedCollectEntity> collectEntityList) {
        List<UserAuthorizedCollect> list = new ArrayList<>();
        for (UserAuthorizedCollectEntity userAuthorizedCollectEntity : collectEntityList) {
            UserAuthorizedCollect userAuthorizedCollect = new UserAuthorizedCollect();
            BeanUtils.copyProperties(userAuthorizedCollectEntity, userAuthorizedCollect);
            list.add(userAuthorizedCollect);
        }
        return list;

    }

    @Override
    public Response<UserAuthorizedCollect> getByUserCodeAndType(String userCode, AuthCollectTypeEnum authCollectTypeEnum) {
        if (StringUtils.isEmpty(userCode)) {
            throw new ApiErrorException("user code is empty");
        }
        if (authCollectTypeEnum == null) {
            throw new ApiErrorException("type is empty");
        }

        UserAuthorizedCollectEntity collectEntity = userAuthorizedCollectDao.selectByUserCodeAndType(userCode, authCollectTypeEnum.getType());
        if (collectEntity == null) {
            return Response.ok().putData(null);
        }
        UserAuthorizedCollect userAuthorizedCollect = new UserAuthorizedCollect();
        BeanUtils.copyProperties(collectEntity, userAuthorizedCollect);
        return Response.ok().putData(userAuthorizedCollect);
    }

    @Override
    public Response<List<UserAuthorizedCollect>> getByTypeAndStatus(AuthCollectTypeEnum authCollectTypeEnum, AuthCollectStatusEnum authCollectStatusEnum) {
        if (authCollectTypeEnum == null) {
            throw new ApiErrorException("type is empty");
        }
        if (authCollectStatusEnum == null) {
            throw new ApiErrorException("status is empty");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("type", authCollectTypeEnum.getType());
        map.put("status", authCollectStatusEnum.getStatus());
        List<UserAuthorizedCollectEntity> list = userAuthorizedCollectDao.selectByParams(map);
        if (list == null || list.isEmpty()) {
            return Response.ok().putData(null);
        }

        return Response.ok().putData(convertEntityToVO(list));
    }

    @Override
    public Response<List<UserAuthorizedCollect>> getNeedHandleList(AuthCollectTypeEnum authCollectTypeEnum, Integer size) {
        if (authCollectTypeEnum == null) {
            throw new ApiErrorException("type is empty");
        }
        if (size == null) {
            throw new ApiErrorException("size is empty");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("type", authCollectTypeEnum.getType());
        map.put("size", size);
        List<UserAuthorizedCollectEntity> list = userAuthorizedCollectDao.selectNeedHandleList(map);
        if (list == null || list.isEmpty()) {
            return Response.ok().putData(null);
        }

        return Response.ok().putData(convertEntityToVO(list));
    }

    @Override
    public Response addUserAuthorizedCollect(UserAuthorizedCollect userAuthorizedCollect) {
        if (userAuthorizedCollect.getType() == null || AuthCollectTypeEnum.getEnumByType(userAuthorizedCollect.getType()) == null) {
            throw new ApiErrorException("type is incorrect");
        }
        if (StringUtils.isBlank(userAuthorizedCollect.getUserCode())) {
            throw new ApiErrorException("user code is empty");
        }

        UserAuthorizedCollectEntity collectEntity = userAuthorizedCollectDao.selectByUserCodeAndType(
                userAuthorizedCollect.getUserCode(), userAuthorizedCollect.getType());
        if (collectEntity == null) {
            // 新增
            collectEntity = new UserAuthorizedCollectEntity();
            BeanUtils.copyProperties(userAuthorizedCollect, collectEntity);
            userAuthorizedCollectDao.insert(collectEntity);
        } else {
            // 更新
            if (StringUtils.isNotBlank(userAuthorizedCollect.getTaskId())) {
                collectEntity.setTaskId(userAuthorizedCollect.getTaskId());
            }
            if (StringUtils.isNotBlank(userAuthorizedCollect.getDataUrl())) {
                collectEntity.setDataUrl(userAuthorizedCollect.getDataUrl());
            }
            if (userAuthorizedCollect.getStatus() != null) {
                collectEntity.setStatus(userAuthorizedCollect.getStatus());
            }
            userAuthorizedCollectDao.updateByPrimaryKey(collectEntity);
        }

        return Response.ok();
    }
}
